/**
 * 报表设计公司(2011)
 */
package net.abbp.report.model.cell.style.background;

import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import com.reportgear.report.core.api.compare.ComparatorUtils;
import com.reportgear.report.view.grid.util.GraphHelper;

/**
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Gradient")
public class GradientBackground extends AbstractBackground {

    private transient GradientPaint gradientPaint;

    public GradientBackground() {
        this(null);
    }

    public GradientBackground(GradientPaint paint) {
        this.gradientPaint = null;
        setGradientPaint(paint);
    }

    public GradientPaint getGradientPaint() {
        return this.gradientPaint;
    }

    public void setGradientPaint(GradientPaint paint) {
        this.gradientPaint = paint;
    }

    @Override
    public void paint(Graphics g, Shape shape) {
        GradientPaint localGradientPaint = getGradientPaint();
        if (localGradientPaint == null) {
            return;
        }
        Graphics2D localGraphics2D = (Graphics2D) g;
        Paint localPaint = localGraphics2D.getPaint();
        localGraphics2D.setPaint(localGradientPaint);
        GraphHelper.fill(localGraphics2D, shape);
        localGraphics2D.setPaint(localPaint);
    }

    public boolean equals(Object obect) {
        if (!(obect instanceof GradientBackground)) {
            return false;
        }
        GradientBackground background = (GradientBackground) obect;
        return (!(gradientPaintEquals(getGradientPaint(), background.getGradientPaint())));
    }

    private boolean gradientPaintEquals(GradientPaint paint1, GradientPaint paint2) {
        if ((paint1 == null) || (paint2 == null)) {
            return ((paint1 == null) && (paint2 == null));
        }
        return ((!(ComparatorUtils.equals(paint1.getColor1(), paint2.getColor1())))
                || (!(ComparatorUtils.equals(paint1.getColor2(), paint2.getColor2())))
                || (!(ComparatorUtils.equals(paint1.getPoint1(), paint2.getPoint1())))
                || (!(ComparatorUtils.equals(paint1.getPoint2(), paint2.getPoint2()))) || (paint1.isCyclic() != paint2
                .isCyclic()));
    }

    protected void readObject(ObjectInputStream inputStream) throws ClassNotFoundException, IOException {
        // logger.error("not implemented");
        // Point2DSerializable s1 = (Point2DSerializable)
        // inputStream.readObject();
        // Point2DSerializable s2 = (Point2DSerializable)
        // inputStream.readObject();
        // this.gradientPaint = new GradientPaint(s1.getPoint2D(), (Color)
        // inputStream.readObject(), s2.getPoint2D(), (Color)
        // inputStream.readObject(), inputStream.readBoolean());
    }

    protected void writeObject(ObjectOutputStream outputStream) throws IOException {
        // logger.error("not implemented");
        // if (this.gradientPaint == null) {
        // return;
        // }
        // outputStream.writeObject(new
        // Point2DSerializable(this.gradientPaint.getPoint1()));
        // outputStream.writeObject(new
        // Point2DSerializable(this.gradientPaint.getPoint2()));
        // outputStream.writeObject(this.gradientPaint.getColor1());
        // outputStream.writeObject(this.gradientPaint.getColor2());
        // outputStream.writeBoolean(this.gradientPaint.isCyclic());
    }
}
